package net.melon.stock.inst.struts2;

import javax.annotation.Resource;

import net.esj.basic.utils.StringUtils;
import net.esj.basic.utils.view.BaseJsonMessage.MessageLevel;
import net.esj.basic.view.AbstractPluginPageAction;
import net.melon.product.pojo.Product;
import net.melon.product.pojo.Product.PRODUCT_ENABLE;
import net.melon.stock.inst.service.StockService;
import net.melon.stock.pojo.Stock;

public class StockAction extends AbstractPluginPageAction<Stock, Stock>{

	/**
	 * 
	 */
	private static final long serialVersionUID = -4205568966315799633L;
	
	private String regionId;
	
	@Resource
	private StockService stockService;
	
	@Override
	protected void doQueryPage() {
		pagination.getQueryBuilder().fetch("product");
		if(StringUtils.hasText(regionId)){
			pagination.getQueryBuilder().like("regionId", regionId+"%");
		}
		super.doQueryPage();
	}
	
	@Override
	protected void doAdd() throws Exception {
		if(stockService.checkAddStock(entity)){
			stockService.addStock(entity);
		}else{
			writeBaseJsonMessage(getText(SYSTEM_MESSAGE_TITLE,""), "该库存中已经加入该商品", MessageLevel.ERROR, "html");
		}
		
	}
	@Override
	protected void doGetEntity() {
		entity = defaultService.get(Integer.valueOf(id), Stock.class);
	}
	
	@Override
	protected void doUpdate() throws Exception {
		stockService.updateStock(entity);
	}
	
	public PRODUCT_ENABLE[] getProductEnables(){
		return Product.PRODUCT_ENABLE.values();
	}
	
	@Override
	protected void doDelete() throws Exception {
		stockService.deleteStock(Integer.valueOf(id));
	}
	
	public Stock getEntity(){
		return entity;
	}
	
	public void setEntity(Stock entity){
		this.entity = entity;
	}

	public String getRegionId() {
		return regionId;
	}

	public void setRegionId(String regionId) {
		this.regionId = regionId;
	}

}
